package forumTest;

import common.TestUtils;
import net.bytebuddy.asm.Advice;
import org.apache.commons.exec.util.StringUtils;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.condition.DisabledIfSystemProperties;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.net.CacheRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import static java.lang.Thread.sleep;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-11-14
 * Time: 17:51
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ForumIndexTest {
    private static WebDriver driver = null;
    @BeforeAll
    static void setUp() throws InterruptedException {
        // 初始化驱动对象(包含智能等待)
        driver = TestUtils.createDriver();

        // 登录
        // 观察登录页面是否正常访问
        driver.get("http://118.25.26.147:58080/sign-in.html");
        // 登录操作
        driver.findElement(By.cssSelector("#username")).sendKeys("lee");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#check")).sendKeys("code");
        // 点击登录按钮
        driver.findElement(By.cssSelector("#submit")).click();
        WebDriverWait wait = new WebDriverWait(driver, 10);
        // 显示等待直到url转到首页
        wait.until(ExpectedConditions.urlToBe("http://118.25.26.147:58080/index.html"));
        sleep(1000);
    }

    @AfterAll
    static void tearDown() {
        // 关闭驱动
        driver.quit();
    }

    // 校验是否跳转到首页
    @Order(1)
    @ParameterizedTest
    @ValueSource(strings = {"http://118.25.26.147:58080/index.html"})
    void toIndex(String url) {
        String currentUrl = driver.getCurrentUrl();
        Assertions.assertEquals(url,currentUrl);
    }


    // 查看首页帖子列表+查看板块帖子列表
    @Order(2)
    @Test
    void seekArticleList() throws InterruptedException {
        // 获取所有板块元素
        List<WebElement> boards = new ArrayList<>();
        while (boards.size() != 10) {
            boards = driver.findElements(By.cssSelector("#topBoardList > li"));
        }
        for (int i = 0; i < boards.size(); i ++) {
            // 点击对应板块
            boards.get(i).findElement(By.cssSelector("a")).click();

            // 获取所有的article元素
            List<WebElement> articles = driver.findElements(By.cssSelector("#artical-items-body > div"));
            // 帖子数量为空
            if (articles.size() == 0) {
                sleep(500);
                String text = driver.findElement(By.cssSelector("#artical-items-body")).getText();
                Assertions.assertEquals("还没有帖子",text);
            } else {
                // 遍历并比较article中的时间元素
                for (int j = 1; j < articles.size(); j ++) {
                    // 校验是否为降序
                    String beforeTime = articles.get(j-1).
                            findElement(By.cssSelector("div > div.col > div.text-muted.mt-2 > div > div.col > ul > li:nth-child(2)"))
                            .getText();
                    String currentTime = articles.get(j).
                            findElement(By.cssSelector("div > div.col > div.text-muted.mt-2 > div > div.col > ul > li:nth-child(2)"))
                            .getText();
                    // 校验是否按照时间降序排序
                    Assertions.assertTrue(beforeTime.compareTo(currentTime)>0);
                }

                // 校验板块标题和文章数量
                if (i != 0) {
                    String exceptText = boards.get(i).findElement(By.cssSelector("a > span.nav-link-title")).getText();
                    String currentText = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
                    Assertions.assertEquals(exceptText,currentText);

                    int exceptCount = articles.size();
                    int currentCount = driver.findElement(By.cssSelector("#article_list_count_board")).getText().
                            charAt(driver.findElement(By.cssSelector("#article_list_count_board")).getText().length()-1)
                            -'0';
                    Assertions.assertEquals(exceptCount,currentCount);
                }

                // 校验参数
                for (WebElement article : articles) {
                    // 初步校验用户头像(url不为空)
                    String url = article
                            .findElement(By.cssSelector("div > div.col-auto > span")).getAttribute("style")
                            .split("url\\(")[1].split("\\)")[0];
                    Assertions.assertNotNull(url);
                    // 初步校验用户昵称(昵称不为空)
                    String nickName = article
                            .findElement(By.cssSelector("div > div.col > div.text-muted.mt-2 > div > div.col > ul > li:nth-child(1)"))
                            .getText();
                    Assertions.assertNotNull(nickName);
                    // 初步校验帖子标题(不为空)
                    String title = article
                            .findElement(By.cssSelector("div > div.col > div.text-truncate > a > strong"))
                            .getText();
                    Assertions.assertNotNull(title);
                    // 初步校验发帖时间(时间不为空)
                    String time = article
                            .findElement(By.cssSelector("div > div.col > div.text-muted.mt-2 > div > div.col > ul > li:nth-child(2)"))
                            .getText();
                    Assertions.assertNotNull(time);
                    // 初步校验浏览量(为数字)
                    String lookCount = article
                            .findElement(By.cssSelector("div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(1)"))
                            .getText();
                    Assertions.assertTrue(Integer.parseInt(lookCount)>= 0 && Integer.parseInt(lookCount) <= Integer.MAX_VALUE);
                    // 初步校验点赞数(为数字)
                    String likeCount = article
                            .findElement(By.cssSelector("div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(2)"))
                            .getText();
                    Assertions.assertTrue(Integer.parseInt(likeCount)>= 0 && Integer.parseInt(likeCount) <= Integer.MAX_VALUE);
                    // 初步校验回复数(为数字)
                    String replyCount = article
                            .findElement(By.cssSelector("div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3)"))
                            .getText();
                    Assertions.assertTrue(Integer.parseInt(replyCount)>= 0 && Integer.parseInt(replyCount) <= Integer.MAX_VALUE);
                }
            }
        }
    }


    // 发布帖子
    // 发布成功
    @Order(3)
    @ParameterizedTest
    @CsvSource({"2,C++中的那些事","3,前端技术那些事","4,Mysql那些事"})
    void successedPublish(String boardId,String title) throws InterruptedException {
        // 点击发布按钮
        driver
                .findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > " +
                        "div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
        // 根据下拉框选择板块
//        sleep(2000);
        WebElement element = driver.findElement(By.cssSelector("#article_post_borad"));
        // 定义select对象
        Select select = new Select(element);
        // 选择板块
        select.selectByValue(boardId);
        // 输入标题
        driver.findElement(By.cssSelector("#article_post_title")).sendKeys(title);
        // 输入内容
        // 编辑页的md是第三方插件，所以不可以直接使用sendKeys向编辑模块写入内容，但是可以通过点击上方按钮进行内容的插入
        // 插入几条横线
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(
                By.cssSelector("#edit-article > div.editormd-toolbar > div > ul > li:nth-child(21) > a")));
        driver.findElement(By.cssSelector("#edit-article > div.editormd-toolbar > div > ul > li:nth-child(21) > a")).click();
        driver.findElement(By.cssSelector("#edit-article > div.editormd-toolbar > div > ul > li:nth-child(21) > a")).click();
        driver.findElement(By.cssSelector("#edit-article > div.editormd-toolbar > div > ul > li:nth-child(21) > a")).click();

        // 记录发帖时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
        String exceptTime = sdf.format(System.currentTimeMillis());

        // 点击发布按钮
        // 实现滚动到页面底部
        // 这里有一个报错折腾了我好久：ElementClickInterceptedException: element click intercepted: Element is not clickable at point
        // 原因：元素在视窗外，需要全屏滚动条向下
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
//        WebElement webElement = driver.findElement(By.cssSelector("#article_post_submit"));//找到这个元素
//        ((JavascriptExecutor) driver).executeScript("window.scrollTo("+webElement.getLocation().getX()+","+webElement.getLocation().getY()+")");
        sleep(1000);// 等待元素加载
        driver.findElement(By.cssSelector("#article_post_submit")).click();

        // 校验文章标题
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#artical-items-body > div:nth-child(1)")));
        WebElement article= driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1)"));
//        sleep(1000);
        String currentTitle = article
                .findElement(By.cssSelector("div > div.col > div.text-truncate > a > strong"))
                .getText();
        Assertions.assertEquals(title,currentTitle);

        // 校验文章发布时间
        String currentTime = article.
                findElement(By.cssSelector("div > div.col > div.text-muted.mt-2 > div > div.col > ul > li:nth-child(2)"))
                .getText().substring(0,article.
                        findElement(By.cssSelector("div > div.col > div.text-muted.mt-2 > div > div.col > ul > li:nth-child(2)"))
                        .getText().length()-6);
        Assertions.assertEquals(exceptTime,currentTime);
    }

    // 发布失败
    // 帖子标题为空
    @Order(4)
    @Test
    void failedPublish1() throws InterruptedException {
        driver.navigate().refresh();
        // 点击发布按钮
        driver
                .findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > " +
                        "div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
       // 插入几条横线
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(
                By.cssSelector("#edit-article > div.editormd-toolbar > div > ul > li:nth-child(21) > a")));
        // 显示等待直到url转到首页
        wait.until(ExpectedConditions.urlToBe("http://118.25.26.147:58080/index.html"));
        driver.findElement(By.cssSelector("#edit-article > div.editormd-toolbar > div > ul > li:nth-child(21) > a")).click();
        driver.findElement(By.cssSelector("#edit-article > div.editormd-toolbar > div > ul > li:nth-child(21) > a")).click();
        driver.findElement(By.cssSelector("#edit-article > div.editormd-toolbar > div > ul > li:nth-child(21) > a")).click();
        // 点击发布
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
        sleep(1000);// 等待元素加载
        driver.findElement(By.cssSelector("#article_post_submit")).click();

        List<WebElement> elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right"));
        // 校验提示框
        String exceptText = "请输入帖子标题";
        String currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));
    }
    // 帖子内容为空
    @Order(5)
    @Test
    void failedPublish2() throws InterruptedException {
        driver.navigate().refresh();
        // 回到首页
        driver.findElement(By.cssSelector("#nav_board_index > a")).click();
        // 点击发布按钮
        driver
                .findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > " +
                        "div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();

        // 输入标题
        driver.findElement(By.cssSelector("#article_post_title")).sendKeys("test");

        // 等待元素加载
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(
                By.cssSelector("#edit-article > div.editormd-toolbar > div > ul > li:nth-child(21) > a")));

        // 点击发布
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
        sleep(1000);// 等待元素加载
        driver.findElement(By.cssSelector("#article_post_submit")).click();

        // 校验提示框
        String actualText = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        List<WebElement> elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right"));
        String exceptText = "请输入帖子内容";
        String currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));
    }

    // 查看帖子详情
    // 查看个人帖子详情
    @Order(6)
    @Test
    void seekArticleSelf() throws InterruptedException {
        driver.navigate().refresh();
        // 此处java板块中的article都是当前用户的
        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(2) > a")).click();
        sleep(500);
        // 记录查看前阅读量
        String count1 = driver.findElement(By.cssSelector("#artical-items-body > div > div > div.col > div.text-muted.mt-2" +
                " > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(1)")).getText();
        int preCount = Integer.parseInt(count1);

        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a"))
                .click();
        sleep(1000);
        // 校验头像地址：不为空
        String style = driver.findElement(By.cssSelector("#article_details_author_avatar")).getAttribute("style");
        Assertions.assertNotNull(style);
        // 校验用户昵称：lee
        String text = driver.findElement(By.cssSelector("#article_details_author_name")).getText();
        Assertions.assertEquals("lee",text);
        // 校验文章：这里就仅校验标题
        String text1 = driver.findElement(By.cssSelector("#details_article_title")).getText();
        String text2 = driver.findElement(By.cssSelector("#details_article_content_title")).getText();
        Assertions.assertEquals(text2,text1);
        // 校验操作按钮
        String text3 = driver.findElement(By.cssSelector("#btn_details_send_message")).getText();
        Assertions.assertEquals("发私信",text3);
        String text4 = driver.findElement(By.cssSelector("#details_btn_like_count")).getText();
        Assertions.assertEquals("点赞",text4);
        String text5 = driver.findElement(By.cssSelector("#details_artile_edit")).getText();
        Assertions.assertEquals("编辑",text5);
        String text6 = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) >" +
                " div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > " +
                "div > div:nth-child(3) > div > a")).getText();
        Assertions.assertEquals("删除",text6);


        // 获取并校验查看后阅读量
        String count2 = driver.findElement(By.cssSelector("#details_article_visitCount")).getText();
        int afterCount = Integer.parseInt(count2);
        Assertions.assertEquals(preCount+1,afterCount);
    }
    // 查看他人帖子详情
    @Order(7)
    @Test
    void seekArticleOther() throws InterruptedException {
        driver.navigate().refresh();
        // 灌水区的帖子非用户
        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(10) > a")).click();
        sleep(1000);
        // 记录查看前阅读量
        String count1 = driver.findElement(By.cssSelector("#artical-items-body > div > div > div.col > div.text-muted.mt-2" +
                " > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(1)")).getText();
        int preCount = Integer.parseInt(count1);

        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a"))
                .click();
        sleep(1000);
        // 校验头像地址：不为空
        String style = driver.findElement(By.cssSelector("#article_details_author_avatar")).getAttribute("style");
        Assertions.assertNotNull(style);
        // 校验用户昵称：angle
        String text = driver.findElement(By.cssSelector("#article_details_author_name")).getText();
        Assertions.assertEquals("angle",text);
        // 校验文章：这里就仅校验标题
        String text1 = driver.findElement(By.cssSelector("#details_article_title")).getText();
        String text2 = driver.findElement(By.cssSelector("#details_article_content_title")).getText();
        Assertions.assertEquals(text2,text1);
        // 校验操作按钮
        String text3 = driver.findElement(By.cssSelector("#btn_details_send_message")).getText();
        Assertions.assertEquals("发私信",text3);
        String text4 = driver.findElement(By.cssSelector("#details_btn_like_count")).getText();
        Assertions.assertEquals("点赞",text4);

        // 获取并校验查看后阅读量
        String count2 = driver.findElement(By.cssSelector("#details_article_visitCount")).getText();
        int afterCount = Integer.parseInt(count2);
        Assertions.assertEquals(preCount+1,afterCount);
    }

    // 修改帖子
    // 修改成功
    @Order(8)
    @Test
    void successedModifyArticle() throws InterruptedException {
        sleep(1000);
        // 点入lee发布的文章
        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(2) > a")).click();
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a"))
                .click();
        sleep(1000);
        // 点击编辑按钮
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
        WebElement element2 = driver.findElement(By.cssSelector("#details_artile_edit"));
        sleep(1000);
        element2.click();
        // 编辑内容和标题

        String editTitle = "JavaScript 知识扫盲 Test";
        sleep(500);
        // 输入标题
        driver.findElement(By.cssSelector("#edit_article_title")).clear();
        sleep(500);
        driver.findElement(By.cssSelector("#edit_article_title")).sendKeys(editTitle);
        // 输入内容
        // 编辑页的md是第三方插件，所以不可以直接使用sendKeys向编辑模块写入内容，但是可以通过点击上方按钮进行内容的插入
        // 增加几条横线
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(
                By.cssSelector("#edit_article_content_area > div.editormd-toolbar > div > ul > li:nth-child(21) > a")));
        driver.findElement(By.cssSelector("#edit_article_content_area > div.editormd-toolbar > div > ul > li:nth-child(21) > a")).click();
        driver.findElement(By.cssSelector("#edit_article_content_area > div.editormd-toolbar > div > ul > li:nth-child(21) > a")).click();
        driver.findElement(By.cssSelector("#edit_article_content_area > div.editormd-toolbar > div > ul > li:nth-child(21) > a")).click();
        // 点击提交
        js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
        sleep(1000);// 等待元素加载
        WebElement element3 = driver.findElement(By.cssSelector("#edit_article_submit"));
        element3.click();

        // 验证是否修改成功
        sleep(1000);
        WebElement element4 = driver.findElement(By.cssSelector("#topBoardList > li:nth-child(2) > a"));
        element4.click();
        String title = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).getText();
        Assertions.assertEquals(editTitle,title);
    }

    // 修改失败
    // 帖子标题为空
    @Order(9)
    @Test
    void failedModifyArticle() throws InterruptedException {
        // 点入lee发布的文章
        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(2) > a")).click();
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a"))
                .click();
        sleep(1000);
        // 点击编辑按钮
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
        WebElement element2 = driver.findElement(By.cssSelector("#details_artile_edit"));
        sleep(1000);
        element2.click();
        // 编辑内容和标题

        // 输入标题
        sleep(500);
        driver.findElement(By.cssSelector("#edit_article_title")).clear();
        // 输入内容
        // 编辑页的md是第三方插件，所以不可以直接使用sendKeys向编辑模块写入内容，但是可以通过点击上方按钮进行内容的插入
        // 增加几条横线
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(
                By.cssSelector("#edit_article_content_area > div.editormd-toolbar > div > ul > li:nth-child(21) > a")));

        // 点击提交
        js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
        sleep(1000);// 等待元素加载
        WebElement element3 = driver.findElement(By.cssSelector("#edit_article_submit"));
        element3.click();

        sleep(1000);
        // 校验提示框
        List<WebElement> elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right"));
        String exceptText = "请输入帖子标题";
        String currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));
    }
    // 帖子内容为空(暂不实现)

    // 删除帖子（测试删除刚刚发布的3条帖子）
    @Order(10)
    @ParameterizedTest
    @ValueSource(ints = {1,2,3})
    void deleteArticle(Object object) throws InterruptedException {
        // 点击首页
        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(1) > a")).click();
        sleep(500);
        // 点击首页第一篇文章
        WebElement element = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a"));
        element.click();
        sleep(500);
        // 点击删除按钮
        WebElement element1 = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a"));
        element1.click();
        sleep(500);

        // 点击取消
        WebElement element2 = driver.findElement(By.cssSelector("#details_delete_modal > div > div > div.modal-footer > div > div > div:nth-child(1) > a"));
        element2.click();
        sleep(500);

        // 点击删除按钮
        element1.click();
        sleep(500);

        // 点击x
        WebElement element3 = driver.findElement(By.cssSelector("#details_delete_modal > div > div > button"));
        element3.click();
        sleep(500);

        // 点击删除
        element1.click();
        sleep(500);

        // 点击确定
        WebElement element4 = driver.findElement(By.cssSelector("#details_artile_delete"));
        element4.click();
        sleep(500);

        // 校验提示框
        List<WebElement> elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right"));
        String exceptText = "删除成功";
        String currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));
    }

    // 点赞功能
    @Order(11)
    @Test
    void like() throws InterruptedException {
        // 点击首页
        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(1) > a")).click();
        sleep(500);
        // 记录点赞前首页第一篇文章的点赞数量
        String count = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(2)")).getText();
        int likeCount = Integer.parseInt(count);

        // 点击首页第一篇文章
        WebElement element = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a"));
        element.click();
        sleep(500);

        WebElement element1 = driver.findElement(By.cssSelector("#details_btn_like_count"));
        // 在这里点十次赞
        for (int i = 0; i < 10 ; i++) {
            element1.click();
            sleep(500);
        }

        // 记录点赞后的点赞数量
        String count2 = driver.findElement(By.cssSelector("#details_article_likeCount")).getText();
        int likeCountAfter = Integer.parseInt(count2);

        // 断言结果
        Assertions.assertEquals(likeCount+10,likeCountAfter);
    }

    // 回复帖子
    // 回复成功
    @Order(12)
    @Test
    void successedReply() throws InterruptedException {
        // 点击首页
        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(1) > a")).click();
        sleep(500);
        // 记录回复前首页第一篇文章的回复数量
        String count = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3)")).getText();
        int replyCount = Integer.parseInt(count);

        // 点击首页第一篇文章
        WebElement element = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a"));
        element.click();
        sleep(500);

        // 滚动到底部找到回复窗口
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
        sleep(500);
        // 回复几条横线
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(
                By.cssSelector("#article_details_reply > div.editormd-toolbar > div > ul > li:nth-child(21) > a")));
        driver.findElement(By.cssSelector("#article_details_reply > div.editormd-toolbar > div > ul > li:nth-child(21) > a")).click();
        driver.findElement(By.cssSelector("#article_details_reply > div.editormd-toolbar > div > ul > li:nth-child(21) > a")).click();

        // 点击回复按钮
        WebElement element1 = driver.findElement(By.cssSelector("#details_btn_article_reply"));
        sleep(500);
        element1.click();
        sleep(500);

        // 获取评论后的数量
        String count2 = driver.findElement(By.cssSelector("#details_article_replyCount")).getText();
        int afterReplyCount = Integer.parseInt(count2);
        Assertions.assertEquals(replyCount+1,afterReplyCount);

        // 校验提示框
        List<WebElement> elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right"));
        String exceptText = "成功";
        String currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));
    }

    // 回复失败
    // 回复内容为空
    @Order(13)
    @Test
    void failedReply() throws InterruptedException {
        driver.navigate().refresh();
        sleep(100);
        // 点击首页
        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(1) > a")).click();
        sleep(1000);
        // 记录回复前首页第一篇文章的回复数量
        String count = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3)")).getText();
        int replyCount = Integer.parseInt(count);

        // 点击首页第一篇文章
        WebElement element = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a"));
        element.click();
        sleep(500);

        // 滚动到底部找到回复窗口
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
        sleep(500);
        // 回复几条横线
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(
                By.cssSelector("#article_details_reply > div.editormd-toolbar > div > ul > li:nth-child(21) > a")));
        // 不填写回复内容

        // 点击回复按钮
        WebElement element1 = driver.findElement(By.cssSelector("#details_btn_article_reply"));
        sleep(500);
        element1.click();
        sleep(500);

        // 获取评论后的数量
        String count2 = driver.findElement(By.cssSelector("#details_article_replyCount")).getText();
        int afterReplyCount = Integer.parseInt(count2);
        Assertions.assertEquals(replyCount,afterReplyCount);

        // 校验提示框
        List<WebElement> elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right"));
        String exceptText = "请输入回复内容";
        String currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));
    }

    // 查看回复列表
    // 回复信息不为空
    @Order(14)
    @Test
    void peekReplyList() throws InterruptedException {
        driver.navigate().refresh();
        // 点击首页
        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(1) > a")).click();
        sleep(500);

        // 点击首页第一篇文章
        WebElement element = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a"));
        element.click();
        sleep(500);

        // 获取回复列表
        List<WebElement> replys = driver.findElements(By.cssSelector("#details_reply_area"));
        for (int i = 1 ; i < replys.size(); i ++) {
            // 校验是否按照时间降序排序
            String preTime = replys.get(i-1).getText();
            String afterTime = replys.get(i).getText();
            Assertions.assertTrue(preTime.compareTo(afterTime)>0);

        }
        for (WebElement reply : replys) {
            // 校验昵称
            String nickName = reply.findElement(By.cssSelector("div.col-3.card > div > h3 > a")).getText();
            Assertions.assertNotNull(nickName);
            // 内容
            String content = reply.findElement(By.cssSelector("div.col-9.card.card-lg > div.card-body")).getText();
            Assertions.assertNotNull(content);
            // 校验头像地址
            String style = reply.findElement(By.cssSelector("div.col-3.card > div > span")).getAttribute("style");
            Assertions.assertNotNull(style);
            // 校验发私信按钮
            String text = reply.findElement(By.cssSelector("div.col-3.card > div > div > a")).getText();
            Assertions.assertEquals("发私信",text);
        }
    }
    // 回复信息为空
    @Order(15)
    @Test
    void peekNullReply() throws InterruptedException {
        driver.navigate().refresh();
        // 点击首页
        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(1) > a")).click();
        sleep(500);

        // 点击首页第二篇文章
        WebElement element = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(2) > div > div.col > div.text-truncate > a"));
        element.click();
        sleep(500);

        String text = driver.findElement(By.cssSelector("#details_reply_area > p")).getText();
        Assertions.assertEquals("还没有回复，第一个写下回复吧",text);
    }

    // 发送站内信
    // 发送成功
    @Order(16)
    @Test
    void successedSendMessage() throws InterruptedException {
        driver.navigate().refresh();
        // 点击首页
        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(1) > a")).click();
        sleep(500);

        // 点击首页第二篇文章
        WebElement element = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(2) > div > div.col > div.text-truncate > a"));
        element.click();
        sleep(500);

        // 点击发私信按钮
        element = driver.findElement(By.cssSelector("#btn_details_send_message"));
        element.click();
        sleep(500);

        // 点击取消
        WebElement element1 = driver.findElement(By.cssSelector("#index_message_receive_cancel"));
        element1.click();
        sleep(500);

        // 点击发私信按钮
        element = driver.findElement(By.cssSelector("#btn_details_send_message"));
        element.click();
        sleep(500);

        // 点击x号
        WebElement element2 = driver.findElement(By.cssSelector("#index_message_modal > div > div > div.modal-header > button"));
        element2.click();
        sleep(500);

        // 点击发私信按钮
        element = driver.findElement(By.cssSelector("#btn_details_send_message"));
        element.click();
        sleep(500);

        // 写入私信内容
        WebElement element3 = driver.findElement(By.cssSelector("#index_message_receive_content"));
        element3.sendKeys("你好啊！！！");

        // 点击发送按钮
        WebElement element4 = driver.findElement(By.cssSelector("#btn_index_send_message"));
        element4.click();
        sleep(500);

        // 校验弹窗
        List<WebElement> elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right"));
        String exceptText = "发送成功";
        String currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));
    }

    // 输入内容为空，给自己发
    @Order(17)
    @Test
    void failedSendMessage() throws InterruptedException {
        driver.navigate().refresh();
        // 点击首页
        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(1) > a")).click();
        sleep(500);

        // 点击首页第3篇文章
        WebElement element = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(3) > div > div.col > div.text-truncate > a"));
        element.click();
        sleep(500);

        // 点击发私信按钮
        element = driver.findElement(By.cssSelector("#btn_details_send_message"));
        element.click();
        sleep(500);

        // 不输入内容点击发送
        WebElement element1 = driver.findElement(By.cssSelector("#btn_index_send_message"));
        element1.click();
        sleep(500);

        // 校验提示框
        List<WebElement> elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right"));
        String exceptText = "请输入要发送的内容";
        String currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));

        // 点击发私信按钮
        element = driver.findElement(By.cssSelector("#btn_details_send_message"));
        element.click();
        sleep(500);

        // 写入私信内容
        WebElement element3 = driver.findElement(By.cssSelector("#index_message_receive_content"));
        element3.sendKeys("你好啊！！！");

        // 点击发送按钮
        WebElement element4 = driver.findElement(By.cssSelector("#btn_index_send_message"));
        element4.click();
        sleep(500);

        // 校验弹窗
        elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right"));
        exceptText = "不能给自己发送站内信";
        currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));
    }

    // 查看站内信列表、查看站内信详情(未读->已读)、回复站内信(未读/已读->已回复)
    // 初始全部为未读消息(sleep时长不够导致元素找不到)
    @Order(18)
    @Test
    void doMessage() throws InterruptedException {

        // 点击站内信按钮
        WebElement element = driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a"));
        element.click();
        sleep(200);

        // 点击所有未读消息// 踩坑了：页面内容被动态刷新或者重新加载后，之前找到的元素就变得无效了
        List<WebElement> messages = driver.findElements(By.cssSelector("#index_div_message_list > div"));
        for (int i = 0; i < messages.size(); i ++) {
            // 依次点击站未读内信
            String text = driver.findElement(By.cssSelector("#index_div_message_list > div:nth-child(1) > div > " +
                    "div.col.text-truncate > a > span.index_message_item_statue")).getText();
            if (text.equals("[未读]")) {
                // 点击未读站内信
                WebElement link = driver.findElement(By.cssSelector("#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a"));
                sleep(300);
                link.click();
                sleep(300);

                // 点击x号
                WebElement element1 = driver.findElement(By.cssSelector("#index_message_reply_cancel"));
                sleep(300);
                element1.click();
                sleep(300);

            }
        }
        // 重新遍历
        for (int i = 0; i < messages.size(); i ++) {
            // 校验是否变为已读
            String text = driver.findElement(By.cssSelector("#index_div_message_list > div:nth-child(1) > div > " +
                    "div.col.text-truncate > a > span.index_message_item_statue")).getText();
            if (text.equals("[已回复]")) {
                continue;
            }
            sleep(200);
            Assertions.assertEquals("[已读]",text);
        }
        // 变历所有已读消息,依次进行回复
        for (int i = 0 ; i < messages.size(); i ++) {
            // 依次回复已读消息
            String text = driver.findElement(By.cssSelector("#index_div_message_list > div:nth-child(1) > div > " +
                    "div.col.text-truncate > a > span.index_message_item_statue")).getText();
            if (text.equals("[已读]")) {
                // 点击已读站内信
                WebElement link = driver.findElement(By.cssSelector("#index_div_message_list > div:nth-child(1) > div >" +
                        " div.col.text-truncate > a "));
                sleep(300);
                link.click();
                sleep(300);

                // 点击回复按钮
                WebElement element1 = driver.findElement(By.cssSelector("#btn_index_message_reply"));
                element1.click();
                sleep(300);

                // 不输入内容，直接点击发送
                WebElement element2 = driver.findElement(By.cssSelector("#btn_index_send_message_reply"));
                element2.click();
                sleep(300);

                // 校验提示框
                List<WebElement> elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right"));
                String exceptText = "请输入要回复的内容";
                String  currentText = elements.get(elements.size() - 1).getText();
                Assertions.assertTrue(currentText.contains(exceptText));

                // 点击已读站内信
                link = driver.findElement(By.cssSelector("#index_div_message_list > div:nth-child(1) > div >" +
                        " div.col.text-truncate > a "));
                link.click();
                sleep(300);

                // 点击回复按钮
                element1 = driver.findElement(By.cssSelector("#btn_index_message_reply"));
                element1.click();
                sleep(300);

                // 输入回复内容，这里固定为"交个朋友!"
                driver.findElement(By.cssSelector("#index_message_reply_receive_content")).sendKeys("交个朋友!");
                // 点击发送按钮
                element2 = driver.findElement(By.cssSelector("#btn_index_send_message_reply"));
                element2.click();
                sleep(300);

                // 校验提示框
                elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div"));
                exceptText = "回复成功";
                currentText = elements.get(elements.size() - 1).getText();
                Assertions.assertTrue(currentText.contains(exceptText));
            }
        }
        messages = driver.findElements(By.cssSelector("#index_div_message_list > div"));
        for (int i = 0; i < messages.size(); i ++) {
            // 校验是否变为已回复
            String text = driver.findElement(By.cssSelector("#index_div_message_list > div:nth-child(1) > div > " +
                    "div.col.text-truncate > a > span.index_message_item_statue")).getText();
            sleep(100);
            Assertions.assertEquals("[已回复]",text);
        }

        sleep(100);
        WebElement element1 = driver.findElement(By.cssSelector("#index_message_offcanvasEnd > div.offcanvas-header > button"));
        element1.click();
        sleep(200);
    }

    // 白夜间模式切换
    @Order(19)
    @Test
    void switchMode() throws InterruptedException {
        driver.navigate().refresh();
        // 切换到夜间模式
        WebElement element = driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-dark"));
        element.click();
        sleep(2000);

        // 切换到白天模式
        element = driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-light"));
        element.click();
        sleep(1000);
    }

    // 查看用户信息
    // 点击文章详情页的用户昵称/我的帖子进入用户界面
    // 这里设定参数为1时从文章详情页访问，为2时从我的帖子进入
    @Order(20)
    @ParameterizedTest
    @ValueSource(ints = {1,2})
    void peekUserInfo(int flag) throws InterruptedException {
        if (flag == 1) {
            // 点击首页
            driver.findElement(By.cssSelector("#topBoardList > li:nth-child(1) > a")).click();
            sleep(500);

            // 点击首页第一篇文章
            WebElement element = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a"));
            element.click();
            sleep(500);

            // 点击用户名字调转到用户页
            WebElement element1 = driver.findElement(By.cssSelector("#article_details_author_name"));
            element1.click();
            sleep(500);
        } else {
            // 点击我的帖子
            WebElement element1 = driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a"));
            element1.click();
            WebElement element = driver.findElement(By.cssSelector("#index_user_profile"));
            element.click();
            sleep(500);
        }

        // 校验昵称(初步校验不为空)
        String nickName = driver.findElement(By.cssSelector("#profile_nickname")).getText();
        Assertions.assertNotNull(nickName);
        // 校验头像url(初步校验不为空)
        String style = driver.findElement(By.cssSelector("#profile_avatar")).getAttribute("style");
        Assertions.assertNotNull(style);
        // 邮箱校验(这里只对邮箱标签进行校验，因为初始邮箱可以为空)
        String emil = driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col > div > div:nth-child(2)")).getText();
        Assertions.assertNotNull(emil);
        // 注册日期校验(初步校验不为空)
        String data = driver.findElement(By.cssSelector("#profile_createTime")).getText();
        Assertions.assertNotNull(data);
        // 校验个人简介(初步校验不为空)
        String introduce = driver.findElement(By.cssSelector("#profile_remark")).getText();
        Assertions.assertNotNull(introduce);
        // 校验发私信按钮
        String text = driver.findElement(By.cssSelector("#btn_profile_send_message")).getText();
        Assertions.assertNotNull(text);
        // 校验发帖数是否等于实际帖子数
        String count = driver.findElement(By.cssSelector("#profile_articleCount")).getText();
        int exceeptCount = Integer.parseInt(count);
        List<WebElement> elements = driver.findElements(By.cssSelector("#profile_article_body > li > div.card.timeline-event-card > div > div"));
        int currentCount = elements.size();
        Assertions.assertEquals(exceeptCount,currentCount);

        // 校验帖子信息
        List<WebElement> articles = driver.findElements(By.cssSelector("#profile_article_body > li"));

        // 校验参数
        for (WebElement article : articles) {
            // 初步校验帖子标题(不为空)
            String title = article
                    .findElement(By.cssSelector("div.card.timeline-event-card > div > div > div > div > div.text-truncate > a > strong"))
                    .getText();
            Assertions.assertNotNull(title);
            // 初步校验发帖时间(时间不为空)
            String time = article
                    .findElement(By.cssSelector("div.card.timeline-event-card > div > div > div > div > div.text-muted.mt-2 > div > div.col > ul > li"))
                    .getText();
            Assertions.assertNotNull(time);
            // 初步校验浏览量(为数字)
            String lookCount = article
                    .findElement(By.cssSelector("div.card.timeline-event-card > div > div > div > div > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(1)"))
                    .getText();
            Assertions.assertTrue(Integer.parseInt(lookCount)>= 0 && Integer.parseInt(lookCount) <= Integer.MAX_VALUE);
            // 初步校验点赞数(为数字)
            String likeCount = article
                    .findElement(By.cssSelector("div.card.timeline-event-card > div > div > div > div > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(2)"))
                    .getText();
            Assertions.assertTrue(Integer.parseInt(likeCount)>= 0 && Integer.parseInt(likeCount) <= Integer.MAX_VALUE);
            // 初步校验回复数(为数字)
            String replyCount = article
                    .findElement(By.cssSelector("div.card.timeline-event-card > div > div > div > div > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3)"))
                    .getText();
            Assertions.assertTrue(Integer.parseInt(replyCount)>= 0 && Integer.parseInt(replyCount) <= Integer.MAX_VALUE);
        }

        // 校验帖子是否降序排序
        for (int i = 1 ; i < articles.size(); i ++) {
            // 校验是否为降序
            String beforeTime = articles.get(i-1).
                    findElement(By.cssSelector("div.card.timeline-event-card > div > div > div > div > div.text-muted.mt-2 > div > div.col > ul > li"))
                    .getText();
            String currentTime = articles.get(i).
                    findElement(By.cssSelector("div.card.timeline-event-card > div > div > div > div > div.text-muted.mt-2 > div > div.col > ul > li"))
                    .getText();
            // 校验是否按照时间降序排序
            Assertions.assertTrue(beforeTime.compareTo(currentTime)>0);
        }

    }

    // 修改个人信息
    // 由于三者比较相似，这里就一块测了
    // 昵称改成功+邮箱修改成功+电话号码修改成功+个人简介修改成功
    @Order(21)
    @ParameterizedTest
    @CsvSource({"leelee,12345,110,我是一个程序猿","abcd,45678,119,不摸鱼的程序猿","lee,2333618926@qq.com,secrecy,人人都说编程好，不知编程催人老，哥敲的不是代码，是人生！"})
    void successedModifyUserInfo(String nickName,String email,String phone,String introduce) throws InterruptedException {
        // 点击个人中心
        WebElement element1 = driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a"));
        element1.click();
        WebElement element = driver.findElement(By.cssSelector("#index_user_settings"));
        element.click();
        sleep(500);
        // 点击昵称框
        WebElement element2 = driver.findElement(By.cssSelector("#setting_input_nickname"));
        element2.clear();
        sleep(500);
        element2.sendKeys(nickName);
        // 点击修改按钮
        WebElement element3 = driver.findElement(By.cssSelector("#setting_submit_nickname"));
        element3.click();
        // 校验弹窗
        List<WebElement> elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div"));
        String exceptText = "操作成功";
        String currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));
        sleep(100);

        // 点击邮箱框
        WebElement element4 = driver.findElement(By.cssSelector("#setting_input_email"));
        element4.clear();
        sleep(500);
        element4.sendKeys(email);
        // 点击修改按钮
        element3 = driver.findElement(By.cssSelector("#setting_submit_email"));
        element3.click();
        // 校验弹窗
        elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div"));
        exceptText = "操作成功";
        currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));
        sleep(100);

        // 点击手机号框
        WebElement element5 = driver.findElement(By.cssSelector("#setting_input_phoneNum"));
        element5.clear();
        sleep(500);
        element5.sendKeys(phone);
        // 点击修改按钮
        element3 = driver.findElement(By.cssSelector("#setting_submit_phoneNum"));
        element3.click();
        // 校验弹窗
        elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div"));
        exceptText = "操作成功";
        currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));
        sleep(100);

        // 将滚轮滚到底部
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
        sleep(500);
        // 点击个人简介框
        WebElement element6 = driver.findElement(By.cssSelector("#settings_textarea_remark"));
        element6.clear();
        sleep(500);
        element6.sendKeys(introduce);
        sleep(100);
        // 点击修改按钮
        WebElement element7 = driver.findElement(By.cssSelector("#settings_submit_remark"));
        element7.click();
        sleep(100);
        // 校验弹窗
        elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div"));
        exceptText = "操作成功";
        currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));
        sleep(100);

        // 校验用户中心的昵称是否改变
        sleep(500);// 这里暂停0.5秒是为了使更改生效
        String afterNickName = driver.findElement(By.cssSelector("#settings_nickname")).getText();
        Assertions.assertEquals(nickName,afterNickName);

        js.executeScript("window.scrollTo(0, 0)");
        sleep(500);
    }

    // 用户昵称修改失败(关于邮箱和电话个人简介是一样的原理，这里不再写了)
    @Order(22)
    @ParameterizedTest
    @ValueSource(strings = {"lee"})
    void failedModifyNickName(String nickName) throws InterruptedException {
        // 点击个人中心
        WebElement element1 = driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a"));
        element1.click();
        WebElement element = driver.findElement(By.cssSelector("#index_user_settings"));
        element.click();
        sleep(500);
        // lee -> leelee
        WebElement element2 = driver.findElement(By.cssSelector("#setting_input_nickname"));
        element2.clear();
        sleep(500);
        element2.sendKeys(nickName);
        // 点击修改按钮
        WebElement element3 = driver.findElement(By.cssSelector("#setting_submit_nickname"));
        element3.click();
        sleep(200);
        // 校验弹窗
        List<WebElement> elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div"));
        String exceptText = "参数校验失败";
        String currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));
        // 校验用户中心的昵称是否改变
        sleep(500);// 这里暂停0.5秒是为了使更改生效
        String afterNickName = driver.findElement(By.cssSelector("#settings_nickname")).getText();
        Assertions.assertTrue(nickName.equals(afterNickName));
    }


    // 修改头像成功
    @Order(23)
    @ParameterizedTest
    @ValueSource(strings = {"D:/forumImage/编程.png","D:/forumImage/程序员.png"})
    void successedModifyImage(String address) throws InterruptedException {
        // 点击个人中心
        WebElement element1 = driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a"));
        element1.click();
        WebElement element = driver.findElement(By.cssSelector("#index_user_settings"));
        element.click();
        sleep(500);
        // 记录修改前的头像url
        String preImageUrl = driver.findElement(By.cssSelector("#settings_avatar")).getAttribute("style");
        // 修改头像
        WebElement fileInput = driver.findElement(By.cssSelector("#settings_input_chooiceAvatar"));
        fileInput.sendKeys(address);
        sleep(1000);

        // 校验提示框
        List<WebElement> elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div"));
        String exceptText = "修改成功";
        String currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));

        // 获取修改后的头像url
        String afterImageUrl = driver.findElement(By.cssSelector("#settings_avatar")).getAttribute("style");
        // 校验修改后的url是否不等于修改前的url
        Assertions.assertTrue(!preImageUrl.equals(afterImageUrl));
        // 校验修改后的url是否和导航栏的url一致
        // 获取导航栏的头像url
        String indexImageUrl = driver.findElement(By.cssSelector("#index_nav_avatar")).getAttribute("style");
        Assertions.assertTrue(indexImageUrl.equals(afterImageUrl));
    }

    // 修改头像失败
    // 头像类型错误(非.png/.jpg)
    @Order(24)
    @ParameterizedTest
    @ValueSource(strings = {"D:/forumImage/failed1.webp"})
    void failedModifyImage1(String address) throws InterruptedException {
        // 点击个人中心
        WebElement element1 = driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a"));
        element1.click();
        WebElement element = driver.findElement(By.cssSelector("#index_user_settings"));
        element.click();
        sleep(500);
        // 记录修改前的头像url
        String preImageUrl = driver.findElement(By.cssSelector("#settings_avatar")).getAttribute("style");
        // 修改头像
        WebElement fileInput = driver.findElement(By.cssSelector("#settings_input_chooiceAvatar"));
        fileInput.sendKeys(address);
        sleep(1000);

        // 校验提示框
        List<WebElement> elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div"));
        String exceptText = "头像类型错误";
        String currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));

        // 获取操作后的头像url
        String afterImageUrl = driver.findElement(By.cssSelector("#settings_avatar")).getAttribute("style");
        // 获取导航栏的头像url
        String indexImageUrl = driver.findElement(By.cssSelector("#index_nav_avatar")).getAttribute("style");
        // 校验失败后url是否改变
        Assertions.assertEquals(preImageUrl,afterImageUrl);
        Assertions.assertEquals(preImageUrl,afterImageUrl);
    }

    // 头像过大（超过默认1M）
    @Order(25)
    @ParameterizedTest
    @ValueSource(strings = {"D:/forumImage/girl.jpg"})
    void failedModifyImage2(String address) throws InterruptedException {
        // 点击个人中心
        WebElement element1 = driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a"));
        element1.click();
        WebElement element = driver.findElement(By.cssSelector("#index_user_settings"));
        element.click();
        sleep(500);
        // 记录修改前的头像url
        String preImageUrl = driver.findElement(By.cssSelector("#settings_avatar")).getAttribute("style");
        // 修改头像
        WebElement fileInput = driver.findElement(By.cssSelector("#settings_input_chooiceAvatar"));
        fileInput.sendKeys(address);
        sleep(1000);

        // 显示等待
        WebDriverWait wait = new WebDriverWait(driver, 10);
        // 显示等待直到url转到获取到提示窗
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        // 校验提示框
        List<WebElement> elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div"));
        String exceptText = "文件过大，请重新上传";
        String currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));

        // 获取操作后的头像url
        String afterImageUrl = driver.findElement(By.cssSelector("#settings_avatar")).getAttribute("style");
        // 获取导航栏的头像url
        String indexImageUrl = driver.findElement(By.cssSelector("#index_nav_avatar")).getAttribute("style");
        // 校验失败后url是否改变
        Assertions.assertEquals(preImageUrl,afterImageUrl);
        Assertions.assertEquals(preImageUrl,afterImageUrl);
    }

    // 注销
    @Order(26)
    @Test
    void logOut() throws InterruptedException {
        sleep(1000);
        // 点击个人中心
        WebElement element1 = driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a"));
        element1.click();

        // 点击注销
        WebElement element = driver.findElement(By.cssSelector("#index_user_logout"));
        element.click();
        sleep(500);

        // 校验url是否为登录页
        String url = driver.getCurrentUrl();
        Assertions.assertEquals("http://118.25.26.147:58080/sign-in.html",url);
    }


    // 由于上一步进行了注销，这里重新登录一下
    @Order(27)
    @Test
    void relogin() throws InterruptedException {
        sleep(500);
        // 登录
        // 观察登录页面是否正常访问
        driver.get("http://118.25.26.147:58080/sign-in.html");
        // 登录操作
        driver.findElement(By.cssSelector("#username")).sendKeys("lee");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#check")).sendKeys("code");
        // 点击登录按钮
        driver.findElement(By.cssSelector("#submit")).click();
        WebDriverWait wait = new WebDriverWait(driver, 10);
        // 显示等待直到url转到首页
        wait.until(ExpectedConditions.urlToBe("http://118.25.26.147:58080/index.html"));
        sleep(1000);
    }

    // 修改密码
    // 修改失败(原密码错误)
    @Order(28)
    @ParameterizedTest
    @CsvSource({"111111,123,123"})
    void failedModifyPassword1(String oldPassword,String newPassword,String newPasswordRepeated) throws InterruptedException {
        // 点击个人中心
        WebElement element1 = driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a"));
        element1.click();
        WebElement element = driver.findElement(By.cssSelector("#index_user_settings"));
        element.click();
        sleep(500);

        // 将滚轮滚到底部
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
        sleep(500);

        // 点击原密码框
        WebElement element2 = driver.findElement(By.cssSelector("#settings_input_oldPassword"));
        element2.sendKeys(oldPassword);
        sleep(100);
        // 点击新密码框
        WebElement element3 = driver.findElement(By.cssSelector("#settings_input_newPassword"));
        element3.sendKeys(newPassword);
        sleep(100);
        // 点击重复密码框
        WebElement element4 = driver.findElement(By.cssSelector("#settings_input_passwordRepeat"));
        element4.sendKeys(newPasswordRepeated);
        sleep(100);
        // 点击提交按钮
        WebElement element5 = driver.findElement(By.cssSelector("#settings_submit_password"));
        element5.click();
        sleep(100);
        // 校验提示框
        List<WebElement> elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div"));
        String exceptText = "原密码错误!";
        String currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));
    }
    // 修改失败(两次输入密码不一致)
    @Order(29)
    @ParameterizedTest
    @CsvSource({"123456,123,1234"})
    void failedModifyPassword2(String oldPassword,String newPassword,String newPasswordRepeated) throws InterruptedException {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollTo(0, 0)");
        sleep(500);

        // 点击个人中心
        WebElement element1 = driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a"));
        element1.click();
        WebElement element = driver.findElement(By.cssSelector("#index_user_settings"));
        element.click();
        sleep(500);

        // 将滚轮滚到底部
        js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
        sleep(500);

        // 点击原密码框
        WebElement element2 = driver.findElement(By.cssSelector("#settings_input_oldPassword"));
        element2.sendKeys(oldPassword);
        sleep(100);
        // 点击新密码框
        WebElement element3 = driver.findElement(By.cssSelector("#settings_input_newPassword"));
        element3.sendKeys(newPassword);
        sleep(100);
        // 点击重复密码框
        WebElement element4 = driver.findElement(By.cssSelector("#settings_input_passwordRepeat"));
        element4.sendKeys(newPasswordRepeated);
        sleep(100);
        // 点击提交按钮
        WebElement element5 = driver.findElement(By.cssSelector("#settings_submit_password"));
        element5.click();
        sleep(100);
        // 校验提示框
        List<WebElement> elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div"));
        String exceptText = "两次输入的密码不相同";
        String currentText = elements.get(elements.size() - 1).getText();
        Assertions.assertTrue(currentText.contains(exceptText));
    }

    // 修改成功
    @Order(30)
    @ParameterizedTest
    @CsvSource({"123456,123,123"})
    void successedModifyPassword(String oldPassword,String newPassword,String newPasswordRepeated) throws InterruptedException {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollTo(0, 0)");
        sleep(500);
        // 点击个人中心
        WebElement element1 = driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a"));
        element1.click();
        WebElement element = driver.findElement(By.cssSelector("#index_user_settings"));
        element.click();
        sleep(500);

        // 将滚轮滚到底部
        js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
        sleep(500);

        // 点击原密码框
        WebElement element2 = driver.findElement(By.cssSelector("#settings_input_oldPassword"));
        element2.sendKeys(oldPassword);
        sleep(100);
        // 点击新密码框
        WebElement element3 = driver.findElement(By.cssSelector("#settings_input_newPassword"));
        element3.sendKeys(newPassword);
        sleep(100);
        // 点击重复密码框
        WebElement element4 = driver.findElement(By.cssSelector("#settings_input_passwordRepeat"));
        element4.sendKeys(newPasswordRepeated);
        sleep(100);
        // 点击提交按钮
        WebElement element5 = driver.findElement(By.cssSelector("#settings_submit_password"));
        element5.click();
        sleep(200);

        // 校验url是否为登录页
        String url = driver.getCurrentUrl();
        Assertions.assertEquals("http://118.25.26.147:58080/sign-in.html",url);
    }

}